import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * 
 */

/**
 * @author keithcasamento
 *
 */
public class Quote
{
    private float stockPrice;
    /**
     * Looks up price of a company's stock
     * @param String stock symbol
     */
    Quote(String s)
    {
            this.price(s);
    }

    /**
     *
     * @param String stock symbol
     * @return int 1 if stock symbol is found, 0 otherwise
     */
    public int price(String sym)
    {
        try{
            sym = sym.toLowerCase();
            URL yahoofin = new URL("http://finance.yahoo.com/d/quotes.csv?s="+sym
                            + "&f=sl1d1t1c1ohgv&e=.csv");
            URLConnection yc = yahoofin.openConnection();
            BufferedReader in = new BufferedReader(
                            new InputStreamReader(yc.getInputStream()));
            String lineIn;
            while( (lineIn = in.readLine()) != null)
            {
                String[] yahooStockInfo = lineIn.split(",");
                stockPrice = Float.valueOf(yahooStockInfo[1]);
                if(stockPrice == 0.00)
                    return 0;
                break;
            }
            in.close();
            return 1;
        }catch(Exception ex){
                return 0;
        }
    }

    /**
     *
     * @return float the price of stock
     */
    public float getPrice(){
        return stockPrice;
    }

}
